//
//  RootViewController.swift
//  feiyu444
//
//  Created by hanweiguang on 2018/9/28.
//  Copyright © 2018年 hanweiguang. All rights reserved.
//

import UIKit
import SnapKit
import  Alamofire

class RootViewController: UIViewController {
    
    var formView: UIView! //登陆框视图
    var horizontalLine:UIView!
    var topConstraint: Constraint? //登录框距顶部距离约束
    var txtUser: UITextField! //用户名输入框
    var txtPwd: UITextField! //密码
    var confirmButton:UIButton! //登陆按钮
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置背景颜色
        self.view.backgroundColor = UIColor(red: 1/255, green: 170/255, blue: 235/255,alpha: 1)
        title="登陆"
        let formViewHeight = 90
        self.formView=UIView()
        self.formView.layer.borderWidth=0.5
        self.formView.layer.borderColor=UIColor.lightGray.cgColor
        self.formView.backgroundColor=UIColor.white
        self.formView.layer.cornerRadius=5
        self.view.addSubview(self.formView)
        
        
        self.formView.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            //存储top属性
            self.topConstraint = make.centerY.equalTo(self.view).constraint
            make.height.equalTo(formViewHeight)
        }
        
        
        //用户名输入框
        self.txtUser = UITextField()
        
        //添加左侧图标
        let imgLock1 = UIImageView(frame:CGRect(x: 11, y: 11, width: 22, height: 22))
        imgLock1.image = UIImage(named:"login_phone")
        
        
        self.txtUser.placeholder = "请输入账号"
        self.txtUser.text = "admin"
        self.txtUser.tag = 100
        self.txtUser.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 44, height: 44))
        self.txtUser.returnKeyType = UIReturnKeyType.next
        
        
        self.txtUser.leftView?.addSubview(imgLock1)
        self.txtUser.leftViewMode = UITextField.ViewMode.always
        
        
        self.formView.addSubview(self.txtUser)
        //布局
        self.txtUser.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(44)
            make.centerY.equalTo(self.formView).offset(-formViewHeight/4)
        }
        
        //分割线
        self.horizontalLine = UIView()
        self.horizontalLine.backgroundColor = UIColor.lightGray
        self.formView.addSubview(self.horizontalLine)
        self.horizontalLine.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(0.5)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.centerY.equalTo(self.formView)
        }
        
        
        //添加左侧图标
        let imgPass = UIImageView(frame:CGRect(x: 11, y: 11, width: 22, height: 22))
        imgPass.image = UIImage(named:"login_key")
        
        //密码输入框
        self.txtPwd = UITextField()
        self.txtPwd.isSecureTextEntry = true
    
        self.txtPwd.placeholder = "密码"
        self.txtPwd.text = "123456"
        self.txtPwd.tag = 101
        self.txtPwd.leftView = UIView(frame:CGRect(x: 0, y: 0, width: 44, height: 44))
        self.txtPwd.leftViewMode = UITextField.ViewMode.always
        self.txtPwd.returnKeyType = UIReturnKeyType.next
        self.txtPwd.leftView?.addSubview(imgPass)
        self.formView.addSubview(self.txtPwd)
        self.txtPwd.snp.makeConstraints { (make) -> Void in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(44)
            make.centerY.equalTo(self.formView).offset(formViewHeight/4)
        }
        
        self.confirmButton=UIButton()
        
        self.confirmButton.setTitle("登陆", for: .normal)
        self.confirmButton.backgroundColor = UIColor.color(hexString: "#66ff55",alpha:0.5)
        self.confirmButton.layer.cornerRadius=5
        
        //        self.formView.addSubview(self.confirmButton)
        self.view.addSubview(self.confirmButton)
        self.confirmButton.addTarget(self, action: #selector(self.buttonClick), for: .touchUpInside)
        
        self.confirmButton.snp.makeConstraints{ (make)-> Void in

            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(self.formView.snp.bottom).offset(20)
            make.height.equalTo(44)
            
            
        }
     
    }
    
    //button 事件
    @objc func buttonClick() {
        
       var pass = txtPwd.text
       var name = txtUser.text
        ZJNetworkProvider.shared.requestDataWithTargetJSON(target:Api.postLogin(name: name!,pass: pass!),  successClosure: {(response) in
            
   //以堆栈方式控制控制器
//         let vc = IndexController()
//          self.navigationController?.pushViewController(vc, animated: true)
//           self.dismiss(animated: true, completion:nil)
        
            //把indexcontroller 切换到根控制器
            
            let vc = IndexController()
            let nav = TiptoesNavController.init(rootViewController: vc)
            let window = UIApplication.shared.keyWindow
             window?.rootViewController = nav
            
        }, failClosure: {_ in
              print("请求失败")
        })



        
        
    }
    

    
}
